//
//  AppState_Settings.swift
//  PokeMaster
//
//  Created by 李腾芳 on 2022/1/11.
//  Copyright © 2022 OneV's Den. All rights reserved.
//

import Foundation
import Combine
import SwiftUI

extension AppState {
    class Settings {
        enum AccountBehavior: CaseIterable {
            case login
            case register
        }
        
        enum Sorting: CaseIterable {
            case id
            case name
            case color
            case favorite
        }
        
        class AccountChecker {
            @Published var accountBehavior = AccountBehavior.login
            @Published var email = ""
            @Published var password = ""
            @Published var verifyPassword = ""
            
            var emailIsValid: AnyPublisher<Bool, Never> {
                $email
                    .debounce(for: .milliseconds(500), scheduler: DispatchQueue.main)
                    .removeDuplicates()
                    .flatMap { email -> AnyPublisher<Bool,Never> in
                        let canSkip = self.accountBehavior == .login
                        let isEmail = email.contains("@")
                        switch (canSkip, isEmail) {
                        case (_ , false):
                            return Just(false).eraseToAnyPublisher()
                        case (true, true):
                            return Just(true).eraseToAnyPublisher()
                        case (false, true): // need valid email from service
                            return EmailValidRequest(email: email)
                                .publisher
                        }
                    }
                    .eraseToAnyPublisher()
            }
            
            var passwordValid: AnyPublisher<Bool, Never> {
                $password.combineLatest($verifyPassword)
                    .map {
                        $0.0 == $0.1 && !$0.0.isEmpty
                    }
                    .eraseToAnyPublisher()
            }
            
            var registerButtonEnable: AnyPublisher<Bool, Never> {
                emailIsValid.combineLatest(passwordValid)
                    .map { $0.0 == true && $0.1 == true }
                    .eraseToAnyPublisher()
            }
        }
        
        
        @FileStorage(directory: .documentDirectory, fileName: "user.json")
        var user: User?
        
        @UserDefaultsStorage(key: "showEnglishName", defaultValue: true)
        var showEnglishName: Bool
        var showFavoriteOnly: Bool = false
        var sorting = Sorting.id
        
        var loginRequested = false
        var settingsError: AppError?
        var accountChecker = AccountChecker()
        var emailIsValid: Bool = false
        var registerButtonEnable = false
        var alertRegisterDialog = false
        var registerAccountRequested = false
    }
}

extension AppState.Settings.Sorting {
    var text: String {
        switch self {
        case .id:
            return "ID"
        case .name:
            return "名字"
        case .color:
            return "颜色"
        case .favorite:
            return "最爱"
        }
    }
}

extension AppState.Settings.AccountBehavior {
    var text: String {
        switch self {
        case .login:
            return "登陆"
        case .register:
            return "注册"
        }
    }
}
